﻿using CoreDomain.BusinessCore;
using DomainService.IService;
using DomainService.IService.Sys;
using Infrastructure;
using Infrastructure.IRepository.Sys;
using Model.BusinessModel.OutPut;
using Model.BusinessModel.OutPut.sys;
using Model.DomainModel.Sys;
using Utility;
using Utility.Enum;

namespace DomainService.Service.Sys
{
    /// <summary>
    /// 按钮样式服务接口实现
    /// </summary>
    public class SysButtonStyleService : ISysButtonStyleService
    {
        #region 构造实例化

        //仓储接口
        private readonly ISysButtonStyleRepository _sysButtonStyleRepository;

        /// <summary>
        /// 基础服务
        /// </summary>
        private readonly IBaseService _baseService;

        /// <summary>
        /// 构造函数 实现依赖注入
        /// </summary>
        public SysButtonStyleService(ISysButtonStyleRepository sysButtonStyleRepository, IBaseService baseService)
        {
            _sysButtonStyleRepository = sysButtonStyleRepository;
            _baseService = baseService;
        }

        #endregion

        #region 服务实现

        /// <summary>
        /// 新增按钮样式
        /// </summary>
        /// <param name="sysButtonStyle"></param>
        public ReceiveStatus Insert(SysButtonStyle sysButtonStyle)
        {
            ReceiveStatus receiveStatus = new ReceiveStatus();
            var result = Validate(sysButtonStyle);
            if (!result.success)
                return result;
            sysButtonStyle.ButtonStyleId = Guid.NewGuid().ToString();
            _sysButtonStyleRepository.Insert(sysButtonStyle, BaseSqlRepository.sysButtonStyle_insertSql);
            return receiveStatus;
        }

        /// <summary>
        /// 修改按钮样式
        /// </summary>
        /// <param name="sysButtonStyle"></param>
        public ReceiveStatus Update(SysButtonStyle sysButtonStyle)
        {
            ReceiveStatus receiveStatus = new ReceiveStatus();
            var result = Validate(sysButtonStyle);
            if (!result.success)
                return result;
            _sysButtonStyleRepository.Update(sysButtonStyle, BaseSqlRepository.sysButtonStyle_updateSql);

            return receiveStatus;
        }

        /// <summary>
        /// 删除按钮样式
        /// </summary>
        /// <param name="buttonStyleId"></param>
        public ReceiveStatus Delete(string buttonStyleId)
        {
            ReceiveStatus receiveStatus = new ReceiveStatus();
            var useCount = _sysButtonStyleRepository.GetButtonStyleCount(buttonStyleId);
            if (useCount > 0)
            {
                return ExceptionHelper.CustomException("删除的当前样式,已经被使用");
            }
            _sysButtonStyleRepository.Delete(buttonStyleId, BaseSqlRepository.sysButtonStyle_delete);

            return receiveStatus;
        }

        /// <summary>
        /// 验证
        /// </summary>
        /// <param name="sysButton"></param>
        /// <returns></returns>
        public ReceiveStatus Validate(SysButtonStyle sysButtonStyle)
        {
            ReceiveStatus receiveStatus = new ReceiveStatus();
            /*
            新增/修改前验证
            1、样式名称不能重复
            */
            var sysButtonStyleModel = _sysButtonStyleRepository.GetSysButtonStyleByName(sysButtonStyle.ButtonStyleName);
            if (sysButtonStyleModel != null)
            {
                if (sysButtonStyle.ButtonStyleName == sysButtonStyleModel.ButtonStyleName && sysButtonStyle.ButtonStyleId.ToLower() != sysButtonStyleModel.ButtonStyleId.ToLower())
                    return ExceptionHelper.CustomException(string.Format("按钮样式名称为【{0}】已存在,请重新输入", sysButtonStyle.ButtonStyleName));
            }

            return receiveStatus;
        }

        /// <summary>
        /// 获取按钮样式数据
        /// </summary>
        /// <returns>返回按钮样式数据</returns>
        public List<SysButtonStyleOutPut> GetButtonStyleList()
        {
            var result = _baseService.GetAuthWhere(RuleTypeEnum.DataRowRule, "bs");
            string? authWhere = string.Empty;
            if (result.success) { authWhere = result.data[0]; }
            else
                ExceptionHelper.ThrowBusinessException(result.msg);

            return _sysButtonStyleRepository.GetButtonStyleList(authWhere);
        }

        /// <summary>
        /// 获取所有按钮样式
        /// </summary>
        /// <returns></returns>
        public List<SelectOutPut> GetButtonSelect()
        {
            List<SelectOutPut> selectOutPuts = [];
            var list = _sysButtonStyleRepository.GetAll(BaseSqlRepository.sysButtonStyle_selectAllSql);
            foreach (var item in list)
            {
                SelectOutPut selectOutPut = new()
                {
                    value = item.ButtonStyleId ?? string.Empty,
                    label = item.ButtonStyleName ?? string.Empty,
                    disabled = item.IsDisabled,
                };
                selectOutPuts.Add(selectOutPut);
            }
            return selectOutPuts;
        }
        #endregion
    }
}
